<?php
/**
 * Created by PhpStorm.
 * User: Mac
 * Date: 2019-01-12
 * Time: 20:06
 */
$server=new swoole_http_server("0.0.0.0",9504);
//设置显示静态文件
$server->set(
    [
        'enable_static_handler'=>true,
        'document_root'=>'/home/wwwroot/default/tp5/public/static/',
        'worker_num'=>5,

    ]
);

$server->on('WorkerStart',function ($ser,$worder_id){
    //定义应用常量
    define('APP_PATH', __DIR__ . '/../application/');
    // ThinkPHP 引导文件
    // 加载基础文件
    require __DIR__ . '/../thinkphp/base.php';

});

$server->on('request',function ($request,$response)use ($server){
    $_SERVER=[];
    if(isset($request->server)){
        foreach ($request->server as $k=>$v){
            $_SERVER[strtoupper($k)]=$v;
        }
    }
    if(isset($request->header)){
        foreach ($request->header as $k=>$v){
            $_SERVER[strtoupper($k)]=$v;
        }
    }
    $_GET=[];
    if(isset($request->get)){
        foreach ($request->get as $k=>$v){
            $_GET[$k]=$v;
        }
    }
    if(empty($_GET)){
        unset($_GET);
    }
    $_POST=[];
    if(isset($request->post)){
        foreach ($request->post as $k=>$v){
            $_POST[$k]=$v;
        }
    }
    ob_start();
// 执行应用并响应
    try{
        think\Container::get('app', [defined('APP_PATH') ? APP_PATH : ''])
            ->run()
            ->send();
    }catch (\Exception $e){

    }

    $res=ob_get_contents();
    ob_clean();
    echo request()->action().PHP_EOL;
    $response->cookie('wjp','wujiangpan',time()+3600);
    $response->end($res);
    //$server->close();//暴力关闭
});

$server->start();
